﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace FigureDraw
{
    public enum PenType{Thin, Medium, Large}
    public enum DiagramType { BasicShape, FlowChart, DataFlow, Activities}
    public enum DiagramTep { Start, Stop}
    

    public partial class Form1 : Form
    {
        public Target myTarget;
        private bool flagPaint;

        private bool flagbasic=true;
        private bool flagFlow = false;
        private bool flagDataFlow = false;
        private bool flagActivities = false;

        private Vector2 oldPosition;
        private Vector2 curPosition;
        private ArrayList myShapes;
        private Paper paper;
        public Form1()
        {
            InitializeComponent();
            myInit();
        }

        private void myInit()
        {
            flagPaint = false;
            oldPosition = new Vector2();
            curPosition = new Vector2();
            myShapes = new ArrayList();
            paper = new myScreen(this.panelDraw);
        }

        private void panelDraw_Paint(object sender, PaintEventArgs e)
        {
            for (int i = 0; i < myShapes.Count; i++)
            {
                myShape m = (myShape)myShapes[i];
                m.Draw(myTarget, paper, PenType.Thin);
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Drawing.Graphics g1 = panelDraw.CreateGraphics();
            g1.Clear(panelDraw.BackColor);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        private void panelDraw_MouseUp(object sender, MouseEventArgs e)
        {
            curPosition.X = e.Location.X;
            curPosition.Y = e.Location.Y;
            if (radioButtonGDIPlus.Checked)
            {
                myTarget = new GDIPlusAdapter();
            }
            else
            {
                myTarget = new CairoAdapter();
            }

            if (flagPaint == true)
            {
                myShape mShape=null;
                if (flagbasic == true)
                {
                    if (radioButtonLine.Checked == true)
                    {
                        mShape = new myLine(oldPosition, curPosition);
                    }
                    if (radioButtonRectangle.Checked == true)
                    {
                        mShape = new myRectangle(oldPosition, curPosition);
                    }
                    if (radioButtonEclipse.Checked == true)
                    {
                        mShape = new myEclipse(oldPosition, curPosition);
                    }

                }

                //other if

                //rectShape.Draw(myTarget, paper, PenType.Thin);
                mShape.Draw(myTarget, paper, PenType.Thin);
                myShapes.Add(mShape);
            }
            flagPaint = false;
            
        }

        private void panelDraw_MouseDown(object sender, MouseEventArgs e)
        {
            if (flagbasic == true ||
                flagDataFlow == true ||
                flagDataFlow == true ||
                flagActivities == true)
            {
                flagPaint = true;
            }

            oldPosition.X = e.Location.X;
            oldPosition.Y = e.Location.Y;
        }

        private void panelDraw_MouseMove(object sender, MouseEventArgs e)
        {
            
        }

        private string filePath="";
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "PNG (*.png)|*.png|JPEG (*.jpg)|*.ext";
            saveFileDialog1.FilterIndex = 1;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Console.WriteLine(saveFileDialog1.FileName);//Do what you want here
                filePath = saveFileDialog1.FileName;
                myTarget.SavePNG(filePath);
            } 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string path;

            path = textBox1.Text;

            if (path != "")
            {
                myTarget.SavePNG(path);
                MessageBox.Show("Saved to " + path);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = "D:\\Image_Name."+comboBox1.SelectedItem.ToString();
            textBox1.Enabled = true;
        }

        private void basicShapesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            basicShapesToolStripMenuItem.Checked = true;
            flowChartToolStripMenuItem.Checked = false;
            dataFlowDiagramToolStripMenuItem.Checked = false;
            activitiesDiagramToolStripMenuItem.Checked = false;
            flagbasic = true;
            flagFlow = false;
            flagDataFlow = false;
            flagActivities = false;
        }

        private void flowChartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            basicShapesToolStripMenuItem.Checked = false;
            flowChartToolStripMenuItem.Checked = true;
            dataFlowDiagramToolStripMenuItem.Checked = false;
            activitiesDiagramToolStripMenuItem.Checked = false;
            flagbasic = false;
            flagFlow = true;
            flagDataFlow = false;
            flagActivities = false;
        }

        private void dataFlowDiagramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            basicShapesToolStripMenuItem.Checked = false;
            flowChartToolStripMenuItem.Checked = false;
            dataFlowDiagramToolStripMenuItem.Checked = true;
            activitiesDiagramToolStripMenuItem.Checked = false;
            flagbasic = false;
            flagFlow = false;
            flagDataFlow = true;
            flagActivities = false;
        }

        private void activitiesDiagramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            basicShapesToolStripMenuItem.Checked = false;
            flowChartToolStripMenuItem.Checked = false;
            dataFlowDiagramToolStripMenuItem.Checked = false;
            activitiesDiagramToolStripMenuItem.Checked = true;
            flagbasic = false;
            flagFlow = false;
            flagDataFlow = false;
            flagActivities = true;
        }
    }
}
